package gae.google.tcserver.api;

import gae.google.tcserver.model.ActivityBean;
import gae.google.tcserver.model.TeamBean;
import gae.google.tcserver.model.TeamMemberBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//import java.util.logging.Logger;

import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

public class ParseJson {

	/**
	 * constructor
	 */
	public ParseJson() {
	}
	
	/**
	 * get all teams 
	 * 
	 * @param json the json to parse
	 * @return teams a list of teams
	 */
	public List<TeamBean> parseTeams(String json) throws IOException{
		
		List<TeamBean> teams = new ArrayList<TeamBean>();
		long noOfTeams = 0;
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONObject teamsObject = jsonObject.getJSONObject("teams");
			noOfTeams = teamsObject.getLong("noofteams");
			for (long i = 0; i < noOfTeams; i++) {
				TeamBean teamBean = new TeamBean();
				JSONObject team = teamsObject.getJSONObject("team" + Long.toString(i+1));
				teamBean.setId(team.getString("id"));
				teamBean.setName(team.getString("name"));
				teamBean.setImageName(team.getString("image"));
				teams.add(teamBean);
			}
		}catch(JSONException e) {
			teams = null;
		}
		return teams;
	}
	
	/**
	 * get all team members 
	 * 
	 * @param json the json to parse
	 * @return teamMembers a list of all team members
	 */
	public List<TeamMemberBean> parseTeamMembers(String json) throws IOException{
		
		//final Logger log = Logger.getLogger(LoginServlet.class.getName());

		List<TeamMemberBean> teamMembers = new ArrayList<TeamMemberBean>();
		long noOfTeamMembers = 0;
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONObject teamMembersObject = jsonObject.getJSONObject("teammembers");
			noOfTeamMembers = teamMembersObject.getLong("noofteammembers");
			for (long i = 0; i < noOfTeamMembers; i++) {
				TeamMemberBean teamMemberBean = new TeamMemberBean();
				JSONObject teamMember = teamMembersObject.getJSONObject("teammember" + Long.toString(i+1));
				teamMemberBean.setId(teamMember.getString("id"));
				teamMemberBean.setFirstName(teamMember.getString("firstname"));
				teamMemberBean.setFamilyName(teamMember.getString("familyname"));
				teamMemberBean.setAddress(teamMember.getString("address"));
				teamMemberBean.setZipCode(teamMember.getString("zipcode"));
				teamMemberBean.setLocation(teamMember.getString("location"));
				teamMemberBean.setPhoneNumber(teamMember.getString("phonenumber"));
				teamMemberBean.setMailAddress(teamMember.getString("mailaddress"));
				teamMemberBean.setRole(teamMember.getString("role"));
				teamMemberBean.setImageName(teamMember.getString("imagename"));
				teamMembers.add(teamMemberBean);
			}
		}catch(JSONException e) {
			teamMembers = null;
		}
		return teamMembers;
	}
	
	/**
	 * get all activities in a month 
	 * 
	 * @param json the json to parse
	 * @return activities a list of activities
	 */
	public List<ActivityBean> parseActivities(String json) throws IOException{
		
		List<ActivityBean> activities = new ArrayList<ActivityBean>();
		long noOfActivities = 0;
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONObject activityObject = jsonObject.getJSONObject("activities");
			noOfActivities = activityObject.getLong("noofactivities");
			for (long i = 0; i < noOfActivities; i++) {
				ActivityBean activityBean = new ActivityBean();
				JSONObject activity = activityObject.getJSONObject("activity" + Long.toString(i+1));
				activityBean.setId(activity.getString("id"));
				activityBean.setDate(activity.getLong("date"));
				activityBean.setActivity(activity.getString("activity"));
				activities.add(activityBean);
			}
		}catch(JSONException e) {
			activities = null;
		}
		return activities;
	}

}
